/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ChateoFacadeNI;

import ourMessage.Hello;
import ourMessage.Bye;
import ourMessage.ConnexionUDPServer;
import ourMessage.ConnexionUDPClient;
import ourMessage.Text;
import ChateoController.*;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 *
 * @author adrian
 * @author olga
 */
public class FacadeNI {

    private Controller theController;
    private ConnexionUDPServer UDPServer;

    public FacadeNI(Controller father, int port) throws SocketException {
        this.theController = father;
        this.UDPServer = new ConnexionUDPServer(port, this);
    }

    public Controller getTheController() {
        return theController;
    }

    public ConnexionUDPServer getUDPServer() {
        return UDPServer;
    }

    public void setTheController(Controller theController) {
        this.theController = theController;
    }

    public void setUDPServer(ConnexionUDPServer UDPServer) {
        this.UDPServer = UDPServer;
    }

    /**
     *
     * @param remoteLogin the new connected remote user's login
     * @param remoteAddr the new connected remote user's address
     * @throws UnknownHostException
     */
    public void incommingHello(String remoteLogin, InetAddress remoteAddr) throws UnknownHostException {
        theController.newUser(remoteLogin, remoteAddr);
    }

    /**
     *
     * @param addr the new connected local user's login
     * @param myLogin the new connected local user's address
     * @throws UnknownHostException
     * @throws SocketException
     */
    public void outgoingHello(InetAddress addr, String myLogin) throws UnknownHostException, SocketException {
        ConnexionUDPClient sender = new ConnexionUDPClient();
        try {
            sender.sendMessage(addr, new Hello(myLogin, false));
        } catch (Exception e) {
            System.out.println("Tentative d'envoi de message échouée");
        }
        System.out.println("The client" + myLogin + " has been connected. Message sent \n");

    }

    /**
     *
     * @param remoteLogin the leaving remote user's login
     * @param remoteAddr the leaving remote user's address
     * @throws UnknownHostException
     */
    public void incommingBye(String remoteLogin, InetAddress remoteAddr) throws UnknownHostException {
        theController.userLeft(remoteLogin, remoteAddr);
    }

    /**
     *
     * @param addr the leaving local user's address
     * @param myLogin the leaving local user's login
     * @throws UnknownHostException
     * @throws SocketException
     */
    public void outgoingBye(InetAddress addr, String myLogin) throws UnknownHostException, SocketException {
        ConnexionUDPClient sender = new ConnexionUDPClient();
        sender.sendMessage(addr, new Bye(myLogin));

    }

    /**
     *
     * @param remoteLogin the remote sending ACK user's login
     * @param remoteAddr the remote sending ACK user's address
     * @throws UnknownHostException
     */
    public void incommingAck(String remoteLogin, InetAddress remoteAddr) throws UnknownHostException {
        theController.newUserAck(remoteLogin, remoteAddr);
    }

    /**
     *
     * @param addr the local sending ACK user's address
     * @param myLogin the local sending ACK user's login
     * @throws UnknownHostException
     * @throws SocketException
     */
    public void outgoingAck(InetAddress addr, String myLogin) throws UnknownHostException, SocketException {
        ConnexionUDPClient sender = new ConnexionUDPClient();
        sender.sendMessage(addr, new Hello(myLogin, true));
    }

    /**
     *
     * @param remoteAddr the destination user's address
     * @param myLogin the local user's login
     * @param text the text message to send
     * @param privato if yes or no the message is private
     * @throws SocketException
     */
    public void outgoingText(InetAddress remoteAddr, String myLogin, String text, boolean privato) throws SocketException {
        ConnexionUDPClient sender = new ConnexionUDPClient();
        sender.sendMessage(remoteAddr, new Text(myLogin, text, privato));
    }

    /**
     *
     * @param remoteAddr the remote user's address
     * @param remoteLogin the remote user's login
     * @param text the incomming text message
     * @param privato if yes or no the message is private
     */
    public void incommingText(InetAddress remoteAddr, String remoteLogin, String text, boolean privato) {
        theController.newTextMessage(remoteLogin, remoteAddr, text, privato);
    }

}
